
#import "SMDataStore.h"
#import "../Models/SMBanner.h"
#import "../Models/SMMoment.h"
#import "../Models/SMTradeItem.h"

@implementation SMDataStore {
    NSArray<SMBanner *> *_banners;
    NSArray<NSDictionary *> *_homeNews;
    NSMutableArray<SMMoment *> *_moments;
    NSMutableArray<SMTradeItem *> *_supply;
    NSMutableArray<SMTradeItem *> *_demand;
}
+ (instancetype)shared {
    static SMDataStore *s;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{ s = [SMDataStore new]; });
    return s;
}
- (instancetype)init {
    if (self = [super init]) {
        // banners
        SMBanner *b1 = [SMBanner new]; b1.imageURL = @"https://picsum.photos/800/300?1"; b1.linkURL = @"https://example.com/a";
        SMBanner *b2 = [SMBanner new]; b2.imageURL = @"https://picsum.photos/800/300?2"; b2.linkURL = @"https://example.com/b";
        SMBanner *b3 = [SMBanner new]; b3.imageURL = @"https://picsum.photos/800/300?3"; b3.linkURL = @"https://example.com/c";
        _banners = @[b1,b2,b3];
        // news
        _homeNews = @[@{@"title":@"废钢价格上涨 2%", @"time":@"今天 10:20"},
                      @{@"title":@"华东市场到货放缓", @"time":@"今天 09:10"},
                      @{@"title":@"炉料需求回暖", @"time":@"昨天 18:32"}];
        // moments
        _moments = [NSMutableArray array];
        for (int i=0;i<3;i++){ SMMoment *m=[SMMoment new]; m.momentId=[NSString stringWithFormat:@"%d",i]; m.author=@"老张"; m.avatarURL=@"https://i.pravatar.cc/100"; m.text=@"今天收了一批重废，成色不错～"; m.imageURLs=@[@"https://picsum.photos/200?1",@"https://picsum.photos/200?2"]; [_moments addObject:m]; }
        // supply/demand
        _supply = [NSMutableArray array]; _demand = [NSMutableArray array];
        for (int i=0;i<10;i++){ SMTradeItem *t=[SMTradeItem new]; t.itemId=[NSString stringWithFormat:@"s%d",i]; t.type=SMTradeTypeSupply; t.title=@"出售重废"; t.desc=@"含量95%，量大价优"; t.location=@"山东 济南"; t.contact=@"王先生 138****8888"; t.price=@"¥2300/吨"; [_supply addObject:t]; }
        for (int i=0;i<10;i++){ SMTradeItem *t=[SMTradeItem new]; t.itemId=[NSString stringWithFormat:@"d%d",i]; t.type=SMTradeTypeDemand; t.title=@"求购统废"; t.desc=@"长期收购，月度结算"; t.location=@"江苏 苏州"; t.contact=@"李先生 139****6666"; t.price=@"面议"; [_demand addObject:t]; }
    }
    return self;
}


- (NSArray<NSDictionary *> *)homeNewsForCategory:(NSString *)category {
    // 按分类返回不同的本地模拟数据
    if ([category isEqualToString:@"重废"]) {
        return @[
            @{@"title":@"重废出货积极，价格坚挺", @"time":@"今天 09:20"},
            @{@"title":@"华北重废到货下滑", @"time":@"昨天 16:05"},
        ];
    } else if ([category isEqualToString:@"统废"]) {
        return @[
            @{@"title":@"统废含杂偏高，议价空间增大", @"time":@"今天 08:50"},
            @{@"title":@"南方统废走货缓慢", @"time":@"昨天 14:11"},
        ];
    } else if ([category isEqualToString:@"剪切料"]) {
        return @[
            @{@"title":@"剪切料加工费走高", @"time":@"今天 10:05"},
            @{@"title":@"钢厂压价有限", @"time":@"昨天 19:32"},
        ];
    } else if ([category isEqualToString:@"破碎料"]) {
        return @[
            @{@"title":@"破碎料毛料偏紧", @"time":@"今天 09:58"},
            @{@"title":@"沿海区域成交改善", @"time":@"昨天 17:20"},
        ];
    } else if ([category isEqualToString:@"不锈钢"]) {
        return @[
            @{@"title":@"304 不锈钢回收价跟涨", @"time":@"今天 11:12"},
            @{@"title":@"镍价走强支撑不锈钢", @"time":@"昨天 15:40"},
        ];
    }
    // 默认
    return self.homeNews ?: @[];
}


- (NSArray<SMBanner *> *)bannersForCategory:(NSString *)category {
    NSMutableArray *arr = [NSMutableArray array];
    for (int i=0;i<3;i++) {
        SMBanner *b = [SMBanner new];
        b.imageURL = [NSString stringWithFormat:@"https://picsum.photos/800/300?banner-%@-%d",
                      category.length?category:@"all", i];
        b.linkURL  = @"https://example.com/banner";
        [arr addObject:b];
    }
    return arr;
}

- (NSArray<NSDictionary *> *)homeNewsForCategory:(NSString *)category
                                            page:(NSInteger)page
                                        pageSize:(NSInteger)pageSize {
    NSMutableArray *source = [NSMutableArray array];
    NSString *prefix = category.length ? category : @"综合";
    for (NSInteger i=0; i<60; i++) {
        [source addObject:@{
            @"title": [NSString stringWithFormat:@"%@ 行情资讯 #%ld", prefix, (long)i+1],
            @"intro": @"市场到货放缓，钢厂到货压力缓解，短期价格偏强。",   // 小清新简介
            @"time" : (i%2==0) ? @"今天" : @"昨天",
            @"image": [NSString stringWithFormat:@"https://picsum.photos/240/160?news-%@-%ld",
                       prefix, (long)i]
        }];
    }
    NSInteger start = MAX(0, (page - 1) * pageSize);
    if (start >= source.count) return @[];
    NSInteger end = MIN(start + pageSize, (NSInteger)source.count);
    return [source subarrayWithRange:NSMakeRange(start, end - start)];
}

- (NSArray<SMBanner *> *)banners { return _banners; }
- (NSArray<NSDictionary *> *)homeNews { return _homeNews; }
- (NSMutableArray<SMMoment *> *)moments { return _moments; }
- (NSMutableArray<SMTradeItem *> *)supply { return _supply; }
- (NSMutableArray<SMTradeItem *> *)demand { return _demand; }
- (void)addMoment:(SMMoment *)m { if (m) [_moments insertObject:m atIndex:0]; }
@end
